﻿using DevExpress.XtraEditors;
using Lamex.Base;
using Lamex.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mMachine.Common
{
    public class CRUDHelper
    {
        //插入数据库[LamexMES_FAMA].[dbo].[MT_Maintain_Record]
        public static bool Insert_MT_Maintain_Record(string MAINTAIN_NO, string REPORT_PERSON, string REPORT_TIME, string MACHINE_NO,
            string REPORT_STATE, string FAILURE_TYPE, string FAILURE_DESC,string UPDATE_TIME,string UPDATE_USERID)
        {
            string sql = @"INSERT INTO [LamexMES_FAMA].[dbo].[MT_Maintain_Record]
     ([MAINTAIN_NO]
      ,[REPORT_PERSON]
      ,[REPORT_TIME]
      ,[MACHINE_NO]
      ,[REPORT_STATE]
      ,[FAILURE_TYPE]
      ,[FAILURE_DESC]
      ,[UPDATE_TIME]
      ,[UPDATE_USER])
	  VALUES('" + MAINTAIN_NO + "','" + REPORT_PERSON + "','" + REPORT_TIME + "','" + MACHINE_NO + "','" + REPORT_STATE + "','" + FAILURE_TYPE + "','" + FAILURE_DESC + "','" + UPDATE_TIME + "','" + UPDATE_USERID + "')";
            int m = BaseBuildSql.ExecuteNonQuery(sql);
            if (m > 0)
                return true;
            else
                return false;
        }
        //更新数据库[LamexMES_FAMA].[dbo].[MT_Maintain_Record]--故障上报
        public static bool Update_MT_Maintain_Record(string MAINTAIN_NO, string REPORT_PERSON, string REPORT_TIME, string MACHINE_NO,
            string REPORT_STATE, string FAILURE_TYPE, string FAILURE_DESC)
        {
            string sql = @" UPDATE [LamexMES_FAMA].[dbo].[MT_Maintain_Record] SET REPORT_PERSON='" + REPORT_PERSON + "',REPORT_STATE='" + REPORT_STATE + "'," +
                "REPORT_TIME='" + REPORT_TIME + "',MACHINE_NO='" + MACHINE_NO + "',  FAILURE_TYPE='" + FAILURE_TYPE + "',FAILURE_DESC='" + FAILURE_DESC + "' WHERE MAINTAIN_NO='" + MAINTAIN_NO + "'";
            int m = BaseBuildSql.ExecuteNonQuery(sql);
            if (m > 0)
                return true;
            else
                return false;
        }
        //更新数据库[LamexMES_FAMA].[dbo].[MT_Maintain_Record]--故障响应
        public static bool UPdate_MT_Maintain_Respone(string MAINTAIN_NO, string RESPONSE_PERSON, string RESPONSE_TIME, string ACTUAL_STATE,
            string PROCESS_PLAN, string PROCESS_PERSON, string PROCESS_TIME)
        {
            string sql = @" UPDATE [LamexMES_FAMA].[dbo].[MT_Maintain_Record] SET RESPONSE_PERSON='" + RESPONSE_PERSON + "',RESPONSE_TIME='" + RESPONSE_TIME + "'," +
               "ACTUAL_STATE='" + ACTUAL_STATE + "',PROCESS_PLAN='" + PROCESS_PLAN + "',  PROCESS_PERSON='" + PROCESS_PERSON + "',PROCESS_TIME=" + PROCESS_TIME + " WHERE MAINTAIN_NO='" + MAINTAIN_NO + "'";
            int m = BaseBuildSql.ExecuteNonQuery(sql);
            if (m > 0)
                return true;
            else
                return false;
        }
        //更新数据库[LamexMES_FAMA].[dbo].[MT_Maintain_Record]--故障完成
        public static bool UPdate_MT_Maintain_CONFIRM(string MAINTAIN_NO, string CONFIRM_PERSON, string CONFIRM_TIME)
        {
            string sql = @"UPDATE [LamexMES_FAMA].[dbo].[MT_Maintain_Record] SET CONFIRM_PERSON='" + CONFIRM_PERSON + "',CONFIRM_TIME='" + CONFIRM_TIME + "' WHERE MAINTAIN_NO='" + MAINTAIN_NO + "'";
            int m = BaseBuildSql.ExecuteNonQuery(sql);
            if (m > 0)
                return true;
            else
                return false;
        }
        //插入数据库[LamexMES_FAMA].[dbo].[MT_Outsource_Record]
        public static bool Insert_MT_Outsource_Record(string OUTSOURCE_NO, string MAINTAIN_NO, string UPDATE_TIME, string UPDATE_USER)
        {
            //先查询是否存在-存在返回
            string sql_Query = @"SELECT  [OUTSOURCE_NO]  FROM [LamexMES_FAMA].[dbo].[MT_Outsource_Record]
WHERE MAINTAIN_NO='" + MAINTAIN_NO + "'";
            DataTable dataTable = BaseBuildSql.GetDataTableBySql(sql_Query);
            if (dataTable.Rows.Count > 0)
                return false;
            //不存在则插入
            string sql = @"  INSERT INTO [LamexMES_FAMA].[dbo].[MT_Outsource_Record] ([OUTSOURCE_NO]
      ,[MAINTAIN_NO]
	   ,[UPDATE_TIME]
      ,[UPDATE_USER])
	  VALUES('" + OUTSOURCE_NO + "','" + MAINTAIN_NO + "','" + UPDATE_TIME + "','" + UPDATE_USER + "')";
            int m = BaseBuildSql.ExecuteNonQuery(sql);
            if (m > 0)
                return true;
            else
                return false;
        }
        //插入数据库[LamexMES_FAMA].[dbo].[MT_Maintain_Detail]
        public static bool Insert_MT_Maintain_Detail(string strMAINTAIN_NO, string strACTUAL_MAINTAINER, string strACTUAL_FAILURE_TYPE, string strACTUAL_WORK_CONTENT
            , string strMAINTAIN_RESULT, string strFAILURE_CAUSE, string strIMPROVE_PLAN, string strMAINTAIN_REMARK, string strUPDATE_TIME, string strUPDATE_USER, string strFlag)
        {

            string sql_Query = @"SELECT  [MAINTAIN_NO]  FROM [LamexMES_FAMA].[dbo].[MT_Maintain_Detail]
WHERE MAINTAIN_NO='" + strMAINTAIN_NO + "'";
            DataTable dataTable = BaseBuildSql.GetDataTableBySql(sql_Query);
            if (dataTable.Rows.Count > 0)
            {
                string sql = @" UPDATE [LamexMES_FAMA].[dbo].[MT_Maintain_Detail] SET ACTUAL_MAINTAINER='" + strACTUAL_MAINTAINER + "',ACTUAL_FAILURE_TYPE='" + strACTUAL_FAILURE_TYPE + "'," +
                    "ACTUAL_WORK_CONTENT='" + strACTUAL_WORK_CONTENT + "',MAINTAIN_RESULT='" + strMAINTAIN_RESULT + "',FLAG_BLANK='" + strFlag + "',FAILURE_CAUSE='" + strFAILURE_CAUSE + "'," +
                    "IMPROVE_PLAN='" + strIMPROVE_PLAN + "',MAINTAIN_REMARK='" + strMAINTAIN_REMARK + "',UPDATE_TIME='" + strUPDATE_TIME + "',UPDATE_USER='" + strUPDATE_USER + "'" +
                    " WHERE MAINTAIN_NO='" + strMAINTAIN_NO + "'";
                int m = BaseBuildSql.ExecuteNonQuery(sql);
                if (m > 0)
                    return true;
                else
                    return false;
            }
            else
            {
                string sql = @"  INSERT INTO [LamexMES_FAMA].[dbo].[MT_Maintain_Detail](
       [MAINTAIN_NO]
      ,[ACTUAL_MAINTAINER]
      ,[ACTUAL_FAILURE_TYPE]
      ,[ACTUAL_WORK_CONTENT]
      ,[MAINTAIN_RESULT]
      ,[FAILURE_CAUSE]
      ,[IMPROVE_PLAN]
      ,[MAINTAIN_REMARK]
      ,[FLAG_BLANK]
      ,[UPDATE_TIME]
      ,[UPDATE_USER])
	  VALUES('" + strMAINTAIN_NO + "','" + strACTUAL_MAINTAINER + "','" + strACTUAL_FAILURE_TYPE + "','" + strACTUAL_WORK_CONTENT + "','" + strMAINTAIN_RESULT + "'" +
   ",'" + strFAILURE_CAUSE + "','" + strIMPROVE_PLAN + "','" + strMAINTAIN_REMARK + "','" + strFlag + "','" + strUPDATE_TIME + "','" + strUPDATE_USER + "')";
                int m = BaseBuildSql.ExecuteNonQuery(sql);
                if (m > 0)
                    return true;
                else
                    return false;
            }
        }
        //插入数据库[LamexMES_FAMA].[dbo].[MT_Outsource_Record]
        public static bool Insert_MT_Outsource_Record_2(string strOUTSOURCE_NO, string strMAINTAIN_NO, string strOUTSOURCE_FACTORY, string strWORK_CONTENT, string strOUTSOURCE_DATE
            , string strRETURN_DATE, string strMAINTAIN_COST, string strISSUE_STATE, string strUPDATE_TIME, string strUPDATE_USER, string strOUTSOURCE_STATE)
        {
            string sql = @"  INSERT INTO [LamexMES_FAMA].[dbo].[MT_Outsource_Record] ( [OUTSOURCE_NO]
      ,[MAINTAIN_NO]
      ,[OUTSOURCE_FACTORY]
      ,[OUTSOURCE_STATE]
      ,[WORK_CONTENT]
      ,[OUTSOURCE_DATE]
      ,[RETURN_DATE]
      ,[MAINTAIN_COST]
      ,[ISSUE_STATE]
      ,[UPDATE_TIME]
      ,[UPDATE_USER])
      VALUES('" + strOUTSOURCE_NO + "','" + strMAINTAIN_NO + "','" + strOUTSOURCE_FACTORY + "','" + strOUTSOURCE_STATE + "','" + strWORK_CONTENT + "'," +
      "'" + strOUTSOURCE_DATE + "','" + strRETURN_DATE + "'," + strMAINTAIN_COST + "" +
      ",'" + strISSUE_STATE + "','" + strUPDATE_TIME + "','" + strUPDATE_USER + "')";
            int m = BaseBuildSql.ExecuteNonQuery(sql);
            if (m > 0)
                return true;
            else
                return false;
        }
        //更新数据库[LamexMES_FAMA].[dbo].[MT_Outsource_Record]
        public static bool Update_MT_Outsource_Record(string strOUTSOURCE_NO, string strMAINTAIN_NO, string strOUTSOURCE_FACTORY, string strWORK_CONTENT, string strOUTSOURCE_DATE
            , string strRETURN_DATE, string strMAINTAIN_COST, string strISSUE_STATE, string strUPDATE_TIME, string strUPDATE_USER, string strOUTSOURCE_STATE)
        {
            string sql = @"UPDATE [LamexMES_FAMA].[dbo].[MT_Outsource_Record] SET MAINTAIN_NO='" + strMAINTAIN_NO + "'" +
                ",OUTSOURCE_FACTORY='" + strOUTSOURCE_FACTORY + "',OUTSOURCE_STATE='" + strOUTSOURCE_STATE + "',WORK_CONTENT='" + strWORK_CONTENT + "'" +
                ",OUTSOURCE_DATE='" + strOUTSOURCE_DATE + "',RETURN_DATE='" + strRETURN_DATE + "'" +
                ",MAINTAIN_COST=" + strMAINTAIN_COST + ",ISSUE_STATE='" + strISSUE_STATE + "'" +
                ",UPDATE_TIME='" + strUPDATE_TIME + "',UPDATE_USER='" + strUPDATE_USER + "' WHERE OUTSOURCE_NO='" + strOUTSOURCE_NO + "'";
            int m = BaseBuildSql.ExecuteNonQuery(sql);
            if (m > 0)
                return true;
            else
                return false;
        }
        //查询[LamexMES_FAMA].[dbo].[MT_Maintain_Record]
        public static DataTable Get_MT_Maintain_Record(string strMAINTAIN_NO)
        {
            string sql = @"SELECT [RESPONSE_PERSON]
      ,[RESPONSE_TIME]   
      FROM [LamexMES_FAMA].[dbo].[MT_Maintain_Record] WHERE MAINTAIN_NO='" + strMAINTAIN_NO + "'";
            DataTable dataTable = BaseBuildSql.GetDataTableBySql(sql);
            return dataTable;
        }
        //查询[LamexMES_FAMA].[dbo].[MT_Outsource_Record]
        public static DataTable Get_MT_Outsource_Record(string strMAINTAIN_NO)
        {
            string sql = @"SELECT [ISSUE_STATE]
     FROM [LamexMES_FAMA].[dbo].[MT_Outsource_Record] WHERE MAINTAIN_NO='" + strMAINTAIN_NO + "'";
            DataTable dataTable = BaseBuildSql.GetDataTableBySql(sql);
            return dataTable;
        }
        //查询[LamexMESManager].[dbo].FAMA_Account_Location_Info
        public static DataTable Get_FAMA_MachineInfo(string strMACHINE_NO)
        {
            string sql = @"SELECT distinct
b.MACHINE_NO,
b.MACHINE_DESC,
b.MACHINE_NAME,
c.MACHINE_LOCATION,
c.MACHINE_USING_DEPT,
l.PHONE_NO
FROM [LamexMES_FAMA].[dbo].[MT_Maintain_Record] a  join
[LamexMESManager].[dbo].[FAMA_Account_Basic_Info]  b on a.MACHINE_NO=b.MACHINE_NO 
 join [LamexMESManager].[dbo].FAMA_Account_Location_Info c on a.MACHINE_NO=c.MACHINE_NO
 left join  [LamexMES_FAMA].[dbo].[MT_Resource_Worker_List] l on a.REPORT_PERSON=l.WORKER_NAME
WHERE CONFIRM_PERSON IS NULL and b.MACHINE_NO='" + strMACHINE_NO + "'";
            DataTable dataTable = BaseBuildSql.GetDataTableBySql(sql);
            return dataTable;
        }
        //查询设备维护收件人邮箱
        public static DataTable Get_SendMail(string DEPARTMENT)
        {
            string sql = @"SELECT [DEPARTMENT]
      ,[NOTICE_TYPE]
      ,[LEVEL1_RECEIVER]
      ,[LEVEL2_RECEIVER]
      ,[LEVEL3_RECEIVER]
  FROM [LamexMES_FAMA].[dbo].[MT_Mail_Notice_Level]
  WHERE DEPARTMENT='" + DEPARTMENT + "' AND NOTICE_TYPE='响应超时'";
            DataTable dataTable = BaseBuildSql.GetDataTableBySql(sql);
            return dataTable;
        }
        public static bool IsExistList(string dtName, string databaseName)
        {
            string dbName = Loginer.CurrentUser.DBName;
            if (Loginer.CurrentUser.DBName == "正式数据库")
            {
                string Msg = "非授权用户，不允许修改数据！";
                if (string.IsNullOrEmpty(Loginer.CurrentUser.Email))
                {
                    XtraMessageBox.Show(Msg);
                    return false;
                }

                string strWhere = "";
                strWhere += " and DATABASE_NAME='" + databaseName + "' ";
                strWhere += " and TABLE_NAME='" + dtName + "' ";
                if (!string.IsNullOrEmpty(Loginer.CurrentUser.Email))
                {
                    string[] subName = Loginer.CurrentUser.Email.Split('@');
                    //string str = subName[0].Replace('.',' ').Trim().ToUpper();
                    //strWhere += " and UPPER( AUTHORITY_USER_NAME) like '%" + subName[0].ToUpper() + "%'";

                }

                string sql = " select * from Sys_DataAuthority where 1=1 " + strWhere;
                try
                {
                    DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
                    if (dt.Rows.Count > 0)
                    {
                        string[] subName = Loginer.CurrentUser.Email.Split('@');
                        string str = subName[0].Replace('.', ' ').Trim().ToUpper();
                        if (dt.Rows[0]["AUTHORITY_USER_NAME"].ToString().Replace('.', ' ').Trim().ToUpper().Contains(str))
                        {
                            return true;
                        }
                        else
                        {
                            XtraMessageBox.Show(Msg);
                            return false;
                        }
                    }
                    else
                    {
                        XtraMessageBox.Show(Msg);
                        return false;
                    }

                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message);
                    return false;
                }
            }
            else
            {
                return true;
            }

        }
    }
}
